package com.sweets.look.util;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.widget.MarkDialog;
import com.sweets.look.ui.widget.supertoast.SuperActivityToast;

public class ViewUtils {


    public static int convertToDensityPixels(Context context, int densityPixels) {
        return ((int) (densityPixels * context.getResources().getDisplayMetrics().density + 0.5f));
    }

    public static void createInformativeDialog(Context context, String title, String message) {
        new MarkDialog(context).builder().setTitle(title).setMessage(message).setCancelable(true).setPositiveButton(
                context.getResources().getString(R.string.action_ok), null).show();
    }

    public static void dimBackground(Activity activity, float from, float to) {
        final Window window = activity.getWindow();
        ValueAnimator animator = ValueAnimator.ofFloat(from, to);
        animator.setDuration(300);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.alpha = (Float) animation.getAnimatedValue();
                if (wl.alpha == 1.0f) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                } else {
                    window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                }
                window.setAttributes(wl);
            }
        });
        animator.start();
    }

    public static float getDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static int getViewTopColor(View view) {
        Bitmap v0 = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(v0));
        return v0.getPixel(0, 0);
    }

    public static int getWindowHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static int getWindowWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }


    public static void showView(Activity activity, View v, Animation a) {
        PreferenceManager pm = PreferenceManager.getInstance();
        if (v.getVisibility() == View.GONE) {//8
            if (a != null && (pm.getAnimEnabled())) {
                v.startAnimation(a);
            }

            v.setVisibility(View.VISIBLE);
            if (v.getId() == R.id.tabWindow) {
                activity.findViewById(R.id.webview_shade1).setVisibility(View.VISIBLE);
            }
        }
    }

    public static void hideView(Activity activity, View v, Animation anim) {
        PreferenceManager pm = PreferenceManager.getInstance();
        if (v.getVisibility() == View.VISIBLE) {//0
            if (anim != null && (pm.getAnimEnabled())) {
                v.startAnimation(anim);
            }

            v.setVisibility(View.GONE);//8
            if (v.getId() == R.id.tabWindow) {
                activity.findViewById(R.id.webview_shade1).setVisibility(View.GONE);
            }
        }
    }

    public static void initTheme(Application app) {
        if (PreferenceManager.getInstance().getNightModeEnabled()) {
            ThemeUtils.updateNightMode(app, true);
        } else {
            ThemeUtils.updateNightMode(app, false);
        }
    }

    public static boolean isLightColor(int color) {
        boolean v1 = true;
        float[] v0 = new float[3];
        Color.colorToHSV(color, v0);
        if ((((double) v0[2])) < 0.8 || (((double) v0[1])) > 0.2) {
            v1 = false;
        }

        return v1;
    }

    public static boolean isTablet(Context context) {
        switch (context.getResources().getConfiguration().screenLayout & 15) {
            case 3:
            case 4: {
                return true;
            }
        }

        boolean v1 = false;
        return v1;
    }

    public static int measureTextViewHeight(Context context, String text, int textSize) {
        TextView v1 = new TextView(context);
        v1.setText(text);
        v1.setTextSize(2, ((float) textSize));
        //-2147483648
        v1.measure(View.MeasureSpec.makeMeasureSpec(ViewUtils.getWindowWidth(context), MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));// 0 0
        return v1.getMeasuredHeight();
    }

    public static int measureTextViewWidth(Context context, String text, int textSize) {
        TextView v1 = new TextView(context);
        v1.setText(text);
        v1.setTextSize(2, ((float) textSize));
        //-2147483648
        v1.measure(View.MeasureSpec.makeMeasureSpec(ViewUtils.getWindowWidth(context), MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));// 0 0
        return v1.getMeasuredWidth();
    }

    public static int mixTwoColors(int color1, int color2, float amount) {
        float v5 = 1f - amount;
        return -16777216 | ((((int) ((((float) (color1 >> 16 & 255))) * amount + (((float) (color2 >> 16
                & 255))) * v5))) & 255) << 16 | ((((int) ((((float) (color1 >> 8 & 255))) * amount + (((
                float) (color2 >> 8 & 255))) * v5))) & 255) << 8 | (((int) ((((float) (color1 & 255))) *
                amount + (((float) (color2 & 255))) * v5))) & 255;
    }

    public static void setImageTint(int color, ImageView[] views) {
        int v2 = views.length;
        int v1 = 0;

        while (v1 < v2) {
            views[v1].setColorFilter(color);
            ++v1;

        }
    }

    public static void setTextActive(Context context, TextView text, boolean active) {
        PreferenceManager pm = PreferenceManager.getInstance();
        if (active) {
            if (pm.getNightModeEnabled()) {
                text.setTextColor(context.getResources().getColor(R.color.text_third_color));
            } else {
                text.setTextColor(context.getResources().getColor(R.color.accent_color));
            }
        } else {
            if (pm.getNightModeEnabled()) {
                text.setTextColor(context.getResources().getColor(R.color.hint_color));
            } else {
                text.setTextColor(context.getResources().getColor(R.color.hint_color_dark));
            }
        }
    }

    public static void showToast(String msg, Context context) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void showToast(View rootlayout, String message) {
//      new SuperActivityToast(((Activity) context), message);
//      Snackbar.make(rootlayout, message, Snackbar.LENGTH_SHORT)
//                .show();

        Toast.makeText(rootlayout.getContext(), message, Toast.LENGTH_SHORT).show();
    }


    /**
     * 滑动返回上一活动
     *
     * @param v
     * @param context
     */
    public static void swipeToFinish(View v, final Activity context) {
        v.setOnTouchListener(new View.OnTouchListener() {
            long currentTime;
            long downTime;
            int mAction;
            int mLocationX;
            int mX;

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (view != null && !view.hasFocus()) {
                    view.requestFocus();
                }

                this.mAction = event.getAction();
                this.mX = ((int) event.getX());

                if (this.mAction == MotionEvent.ACTION_DOWN) {//按下
                    this.downTime = event.getEventTime();//按下的时间
                    this.mLocationX = this.mX;//按下时x坐标
                } else if (this.mAction == MotionEvent.ACTION_UP) {//1
                    this.currentTime = event.getEventTime();
                    if (this.mX - this.mLocationX > 300 && this.currentTime - this.downTime <= 1500) {
                        context.finish();
                    }

                    this.mLocationX = 0;
                }

                return false;
            }
        });
    }
}

